package com.molichuxing.gateway.api.handler;

import com.molichuxing.framework.result.ResponseResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class ParamExceptionHandler {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Object MethodArgumentNotValidHandler(MethodArgumentNotValidException exception) {
        BindingResult bindingResult = exception.getBindingResult();
        FieldError error = bindingResult.getFieldErrors().get(0);
        String message = error != null ? error.getDefaultMessage() : "空";
        return new ResponseResult<>().error(message);
    }
}
